// $codepro.audit.disable packageNamingConvention

/** Packages */
package edu.gatech.team404;

import java.util.Date;

/**
 * An information holder for a lab.
 * @author Michael Dandy
 * @version 1.0
 */
public class Lab {
	/** The course name that this lab is associated with */
	private String courseName;
	
	/** The unique id of the lab */
	private String id;
	
	/** The name of the lab */
	private String name;
	
	/** The description of the lab */
	private String description;
	
	/** The start date of the lab */
	private Date startDate;
	
	/** The due date of the lab */
	private Date endDate;
	
	/** The team assignment method of the lab */
	private TeamAssignment teamAssignment;
	
	/** The maximum number of in one team */
	private int teamSize;

	/**
	 * Constructor
	 * @param courseName the name of the course this lab is associated with
	 * @param id the id of the lab. It can be null. If so, an ID will be generated.
	 * @param name the name of the lab
	 * @param startDate the start date of the lab
	 * @param endDate the end date of the lab
	 * @param teamAssignment the team assignment method
	 * @param teamSize the maximum size of a team
	 */
	public Lab(String courseName, String id, String name, Date startDate, Date endDate, 
		TeamAssignment teamAssignment, int teamSize) {
		this (courseName, null, name, "", startDate, endDate, teamAssignment, teamSize);
	}
	
	/**
	 * Constructor
	 * @param courseName the name of the course this lab is associated with
	 * @param id the id of the lab
	 * @param name the name of the lab
	 * @param description the description of the lab
	 * @param startDate the start date of the lab
	 * @param endDate the end date of the lab
	 * @param teamAssignment the team assignment method
	 * @param teamSize the maximum size of a team
	 */
	public Lab(String courseName, String id, String name, String description, 
			Date startDate, Date endDate, TeamAssignment teamAssignment, int teamSize) {
		this.courseName = courseName;
		
		if (id == null || id.isEmpty()) {
			this.id = generateID (courseName, name);
		}
		else {
			this.id = id;
		}
			
		this.name = name;
		this.description = description;
		this.startDate = startDate;
		this.endDate = endDate;
		this.teamAssignment = teamAssignment;
		this.teamSize = teamSize;
	}
	
	/**
	 * Generate an ID based on the given feed.
	 * @param courseName the name of the course this lab is associated with
	 * @param name the feed for ID
	 * @return the team ID
	 */
	private String generateID (String courseName, String name) {
		//teamID = the first three letter of courseName
		//			+ the first three initial characters + hashcode
		final String[] wordName = name.split(" ");
		String id = "";
		
		if (courseName.length() >= 4) {
			id += courseName.substring(courseName.length() - 4, courseName.length());
		}
		else {
			id += courseName;
		}

		switch (wordName.length) {
			case 1 :
				id += "" + wordName[0].charAt(0);
				break;
			case 2 :
				id += "" + wordName[0].charAt(0) + wordName[1].charAt(0);
				break;
			default :
				id += "" + wordName[0].charAt(0) + wordName[1].charAt(0)
						+ wordName[2].charAt(0);
				break;
		}
		
		id += "-" + hash(name, 3);
				
		if (id.length() >= 11) {
			id = id.substring(0, 11);
		}
		
		return id;
	}
	
	/**
	 * Generate a hash with the specified digit.
	 * @param feed the feed to be hashed
	 * @param digit the number of digit
	 * @return the hash code
	 */
	private int hash (String feed, int digit) {
		final int hash = Math.abs(feed.hashCode());
		return hash % ((int) Math.pow(10, digit));
	}
	
	/**
	 * Get the name of the course this lab is associated with.
	 * @return the name of the course this lab is associated with
	 */
	public String getCourseName() {
		return courseName;
	}
	
	/**
	 * Set the name of the course this lab is associated with.
	 * @param courseName the name of the course this lab is associated with
	 */
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	/**
	 * Get the unique id of this lab.
	 * @return the unique id of this lab
	 */
	public String getId() {
		return id;
	}
	
	/**
	 * Set the unique id of this lab.
	 * @param id the unique id of this lab
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * Get the name of this lab.
	 * @return the name of this lab
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Set the name of this lab.
	 * @param name the name of this lab
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get the description of this lab.
	 * @return the description of this lab
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * Set the description of this lab.
	 * @param description the description of this lab
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Get the start date of this lab.
	 * @return the start date  of this lab
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * Set the start date of this lab.
	 * @param startDate the start date of this lab
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * Get the due date of this lab.
	 * @return the due date  of this lab
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * Set the due date of this lab.
	 * @param endDate the due date of this lab
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * Get the team assignment method of this lab.
	 * @return the team assignment method of this lab
	 */
	public TeamAssignment getTeamAssignment() {
		return teamAssignment;
	}

	/**
	 * Set the team assignment method of this lab.
	 * @param teamAssignment the team assignment method of this lab
	 */
	public void setTeamAssignment(TeamAssignment teamAssignment) {
		this.teamAssignment = teamAssignment;
	}
	
	/**
	 * Get the maximum size of the team.
	 * @return the maximum size of the team
	 */
	public int getTeamSize() {
		return teamSize;
	}

	/**
	 * Set the maximum size of the team
	 * @param teamSize the maximum size of the team
	 */
	public void setTeamSize(int teamSize) {
		this.teamSize = teamSize;
	}

	/**
	 * Print the attributes of this course.
	 * @return the attributes of this course
	 */
	@Override
	public String toString() {
		return "Lab [courseName=" + courseName + ", id=" + id + ", name="
				+ name + ", description=" + description + ", startDate="
				+ startDate + ", endDate=" + endDate + ", teamAssignment="
				+ teamAssignment + "]";
	}
}
